using FireStarterModeller.BO;

namespace SchemaInfo.Test
{
    public class TableInfoBuilder
    {
        private readonly DBTable _dbTable;

        public TableInfoBuilder(DBTable dbTable)
        {
            _dbTable = dbTable;
        }

        public DBTable DBTable
        {
            get { return _dbTable; }
        }

        public DBPrimaryKey AddPrimaryKey(params string[] columnNames)
        {
            DBPrimaryKey dbPrimaryKey = new DBPrimaryKey();
            _dbTable.PrimaryKeyDB = dbPrimaryKey;
            foreach (string columnName in columnNames)
            {
                DBColumn dbColumn = AddColumn(columnName);
                dbColumn.Nullable = false;
                dbPrimaryKey.AddColumnInfo(dbColumn);
            }
            return dbPrimaryKey;
        }

        public DBColumn AddPrimaryKeyColumn()
        {
            return AddPrimaryKeyColumn(_dbTable.TableNameDB + "ID");
        }

        public DBColumn AddPrimaryKeyColumn(string columnName)
        {
            DBColumn dbColumn = AddColumnOfIdType(columnName);
            dbColumn.Nullable = false;
            if (_dbTable.PrimaryKeyDB == null) _dbTable.PrimaryKeyDB =new DBPrimaryKey();
            _dbTable.PrimaryKeyDB.AddColumnInfo(dbColumn);
            return dbColumn;
        }

        private DBColumn AddColumnOfIdType(string columnName)
        {
            return AddColumn(columnName, "CHAR", 38);
        }

        public DBColumn AddColumn(string columnName)
        {
            return AddColumn(columnName, "VARCHAR", 50);
        }

        public DBColumn AddColumn(string columnName, string dataType, int dataLength)
        {
            DBColumn dbColumn = new DBColumn(columnName)
            {
                DataType =  string.Format("{0}({1})", dataType, dataLength),
                DataLength = dataLength,
                Nullable = true
            };
            _dbTable.Columns.Add(dbColumn);
            dbColumn.OrdinalPosition = _dbTable.Columns.Count;
            return dbColumn;
        }

        public DBForeignKey AddForeignKey(string foreignKeyName, DBTable parentDBTable)
        {
            DBForeignKey dbForeignKey = new DBForeignKey
            {
                ForeignKeyName = foreignKeyName,
                ParentTable = _dbTable,
                RelatedTable = parentDBTable
            };
            foreach (DBColumn pkColumn in parentDBTable.PrimaryKeyDB.Columns)
            {
                DBColumn fkDBColumn = AddColumn(pkColumn.ColumnName);
                fkDBColumn.DataType = pkColumn.DataType;
                fkDBColumn.DataLength = pkColumn.DataLength;
                dbForeignKey.ForeignKeyColumns.Add(new DBForeignKeyColumn
                {
                    ParentColumn = fkDBColumn,
                    RelatedColumn = pkColumn
                });
            }
            return dbForeignKey;
        }

        public DBKey AddUniqueKey(string keyName, params string[] columnNames)
        {
            DBKey dbKey = _dbTable.Keys.CreateBusinessObject();
            dbKey.KeyName = keyName;
            foreach (string columnName in columnNames)
            {
                dbKey.AddColumnInfo(AddColumn(columnName));
            }
            return dbKey;
        }

        public DBColumn AddUniqueKeyColumn()
        {
            DBColumn dbColumn = AddColumn(_dbTable.TableNameDB + "_Unique");
            DBKey dbKey = _dbTable.Keys.CreateBusinessObject();
            dbKey.KeyName = _dbTable.TableNameDB + "_UK";
            dbKey.AddColumnInfo(dbColumn);
            return dbColumn;
        }
    }
}
